﻿#include "logfile.h"
#include "commonfuc.h"
#include <QTextCodec>
#include <QTextStream>
#include "qbytearray.h"
#include <QDateTime>
#include <QDir>

logFile::logFile()
{

}

QFile* logFile::g_pFile = NULL;
ConfigFile* logFile::g_pConfig = NULL;
QMutex* logFile::g_pMutex = new QMutex;


void logFile::printLog(QString & logInfo)
{
    if(g_pMutex)
    {
        g_pMutex->lock();
    }

    printLog_Detail(logInfo);

    if(g_pMutex)
    {
        g_pMutex->unlock();
    }
}

void logFile::printLog_Detail(QString & logInfo)
{
    if(logInfo.isEmpty() ||logInfo.isNull())
    {
        return;
    }

    if(NULL == g_pConfig)
    {
         return;
    }

    QString strRoot = CommonFuc::getAppDirPath();
    QString strPath;
    strPath.append(strRoot);
    strPath.append("log");
    QDir logDir;
    if (!logDir.exists(strPath))
    {
        logDir.mkdir(strPath);
    }

    QString strInitFileName =  g_pConfig->m_cfg.logInfo.logFileName;
    if(strInitFileName!=QString("NULL"))
    {
        QString strAppDirPath = CommonFuc::getAppDirPath();
        QString strCurFilePath = QString("");
        strCurFilePath.append(strAppDirPath);
        strCurFilePath.append("log/");
        strCurFilePath.append(strInitFileName);
        strCurFilePath.append(".log");
        if(NULL != g_pFile)
        {
            g_pFile->close();
            delete g_pFile;
            g_pFile = NULL;
        }
        g_pFile = new QFile(strCurFilePath);
        if(NULL == g_pFile)
        {
            return;
        }
    }

    //判断文件是否太大了，是否需要重新开启一个新文件
    if(NULL != g_pFile)
    {
        qint64 nFileSize = g_pFile->size();
        qint64 nMax = g_pConfig->m_cfg.logInfo.logSize * 1024;
        if(nFileSize > nMax)//1024 * 1024 * 5
        {
            g_pFile->close();
            delete g_pFile;
            g_pFile = NULL;
        }
    }


    /*QFile file;
          file.open(stderr, QIODevice::WriteOnly);
          file.write(msg, qstrlen(msg));        // write to stderr
          file.close();*/

    if(NULL == g_pFile)
    {

        QDateTime datetime =  QDateTime::currentDateTime();
        QString strCreateFileTime;
        strCreateFileTime.sprintf("%04d-%02d-%02d %02d%02d%02d",datetime.date().year(),
      datetime.date().month(),datetime.date().day(),datetime.time().hour(),
      datetime.time().minute(),datetime.time().second());

        QString strAppDirPath = CommonFuc::getAppDirPath();
        QString strCurFilePath = QString("");
        strCurFilePath.append(strAppDirPath);
        strCurFilePath.append("log/");
        strCurFilePath.append(strCreateFileTime);
        strCurFilePath.append(".log");
        g_pFile = new QFile(strCurFilePath);
        if(NULL == g_pFile)
        {
            return;
        }

        //记录新日志文件名到配置文件中
        g_pConfig->SetSaveLogFile(strCreateFileTime);
    }

    g_pFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);

    QString strTime = CommonFuc::getCurTime();
    QString strContent = QString("");
    strContent.append(strTime);
    strContent.append(":");
    strContent.append(logInfo);
    //strContent.append("\n");

    //g_pFile->write(strContent.toLatin1().data());
    //g_pFile->close();

    QTextStream ts(g_pFile);
    ts.setCodec(QTextCodec::codecForName("utf-8"));
    ts << strContent << endl;

    g_pFile->close();

    return;

}
